#===============================================================================
# 5_text_study_figure_6.R
# Purpose: To replicate Figure 6 of the paper: "Downstream Analysis of the 
#          Annotated Messages, Based on whose Annotations are used"
#===============================================================================

# PACKAGES
#===============================================================================
library(dplyr)
library(tidyr)
library(ggplot2)
library(broom)

# DATA
#===============================================================================
# - load dataset with the individual message annotations
main <- read.csv("./data/text-experiment-merged-dataset.csv")

# MAIN
#===============================================================================
# - prepare data for plotting
plotdb <- main %>%
  filter(as.numeric(ID_ADHOC) < 151) %>%
  mutate(ideo = ifelse(as.numeric(IDEO) < 5, "Progressive", "Conservative")) %>%
  dplyr::select(-GENDERCODER, -IDEO) %>%
  gather(task, code, -ID_ADHOC, -PID, -SESSION, -MESSPARTY, -ideo) %>%
  group_by(SESSION, ideo, MESSPARTY, task) %>%
  summarise(perc_pos = round(length(which(code == 1)) / n(), 3) * 100) %>%
  as.data.frame() %>%
  rename(training = SESSION, messparty = MESSPARTY) %>%
  mutate(training = recode(training, 
                           `1` = "Basic",
                           `2` = "Intermediate",
                           `3` = "Advanced"),
         training = factor(training, 
                           levels = c("Basic", "Intermediate", "Advanced")),
         messparty = factor(messparty, 
                            levels = c("PVV", "VVD", "PVDA", "GL")),
         task = recode(task,
                       `DIRECTED_AT` = "Directed at",
                       `NEG_TONE` = "Negative\ntone",
                       `CONS_VIEW` = "Conservative\nview",
                       `PROGR_VIEW` = "Progressive\nview",
                       `GENDER` = "Gender\nissue",
                       `ANGRY_FEEL` = "Feel\nangry",
                       `ENTHU_FEEL` = "Feel\nenthusiastic"),
         task = factor(task, levels = c(
           "Directed at",
           "Negative\ntone",
           "Conservative\nview",
           "Progressive\nview",
           "Gender\nissue",
           "Feel\nangry",
           "Feel\nenthusiastic")),
         ideo = paste0(ideo, "\ncoder"))

# - plot
ggplot(plotdb,
       aes(x = messparty, y = perc_pos, fill = ideo)) +
  geom_bar(stat = "identity", position = "dodge", color = "gray30") +
  geom_text(inherit.aes = FALSE,
            data = plotdb %>%
              filter(ideo == "Progressive\ncoder"),
            aes(x = as.numeric(messparty) + 0.25, y = perc_pos + 15,
                label = paste0(perc_pos, "%")), size = 2.75) +
  geom_text(inherit.aes = FALSE,
            data = plotdb %>%
              filter(ideo == "Conservative\ncoder"),
            aes(x = as.numeric(messparty) - 0.25, y = perc_pos + 15,
                label = paste0(perc_pos, "%")), size = 2.75) +
  geom_hline(yintercept = 0) +
  coord_flip() +
  facet_grid(training ~ task) +
  scale_x_discrete("") +
  scale_y_continuous("\nPercentage of messages from each parties that were coded\nas containing each quantity of interest",
                     limits = c(0, 100)) +
  scale_fill_manual("", values = c("gray80", "gray50")) +
  theme(legend.position = "bottom",
        panel.background = element_blank(),
        panel.spacing = unit(1.5, "lines"),
        axis.text.x = element_text(size = 8),
        axis.text.y = element_text(size = 10),
        axis.title.x = element_text(size = 10),
        strip.text = element_text(size = 12, hjust = 0.5, vjust = 1),
        strip.background = element_blank())

ggsave("./results/6_downstream_text_analysis.tiff",
       width = 30, height = 15, units = "cm", device = "tiff", dpi = 300)

## and save as png for tex
##ggsave("./results/6_downstream_text_analysis.png",
##       width = 30, height = 15, units = "cm",  dpi = 300)

